﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using AppOne.db;
using AppOne.data;

namespace AppOne
{
    public partial class live : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager1.RegisterAsyncPostBackControl(Button1);

            //本页面的初始化工作，只有第一次显示的时候，做一次
            if (!IsPostBack)
            {
            }
            this.Label2.Text = DateTime.Now.ToString();
        }

        protected void Button_Click(object sender, EventArgs e)
        {
            this.Label2.Text = DateTime.Now.ToString();
            this.UpdatePanel1.Update();
        }
        //设置表格头
        private void SetTableHeader(string stype = null)
        {
            #region 声明构造表格的变量
            TableRow row;
            TableCell cell = null;
            #endregion

            #region 产生表头
            //产生一个新行
            row = new TableRow();

            //地点
            cell = new TableCell();
            cell.Text = "地点";
            cell.Width = 150;
            row.Cells.Add(cell);
            //时间
            cell = new TableCell();
            cell.Text = "时间(最新)";
            cell.Width = 150;
            row.Cells.Add(cell);

            //
            cell = new TableCell();
            cell.Text = "tt";
            cell.Width = 150;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "pp";
            cell.Width = 150;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "fl";
            cell.Width = 150;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "df";
            cell.Width = 150;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "dh";
            cell.Width = 150;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "mf";
            cell.Width = 150;
            row.Cells.Add(cell);

            cell = new TableCell();
            cell.Text = "mh";
            cell.Width = 150;
            row.Cells.Add(cell);

            row.Font.Bold = true;
            row.BorderStyle = BorderStyle.Double;
            row.BorderColor = System.Drawing.Color.Green;
            Table1.Rows.Add(row);
            #endregion
        }

        //定时刷新页面
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            this.Label2.Text = DateTime.Now.ToString();
            SetTableHeader(null);
            //UpdateUIFromLastData();
            UpdateUIFromLastData2();
            this.UpdatePanel1.Update();
        }

        protected void UpdateUIFromLastData2()
        {
            TableRow row;
            TableCell cell = null;

            List<jlrd> list = DB.QueryLastData();

            foreach (jlrd j in list)
            {
                //产生一个新行
                row = new TableRow();

                //向里面加入数据
                cell = new TableCell();
                cell.Text = j.Name_char.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Dt_date.ToShortDateString() + " " + j.Tm_time.ToShortTimeString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Tt_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Pp_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Fl_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Df_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Dh_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Mf_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Mh_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);


                Table1.Rows.Add(row);
            }
        }

        protected void UpdateUIFromLastData()
        {
            TableRow row;
            TableCell cell = null;

            List<jlrd> list = DB.QueryLastData();
            
            foreach (jlrd j in list)
            {
                //产生一个新行
                row = new TableRow();

                //向里面加入数据
                cell = new TableCell();
                cell.Text = j.Name_char.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Dt_date.ToString() + " " + j.Tm_time.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Tt_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Pp_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Fl_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Df_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Dh_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Mf_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);

                cell = new TableCell();
                cell.Text = j.Mh_float.ToString();
                cell.Width = 150;
                row.Cells.Add(cell);
                
                
                Table1.Rows.Add(row);
            }
        }
    }
}